package com.whut.transportation.overweight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.whut.transportation.CarCheckActivity;
import com.whut.transportation.R;
import com.whut.transportation.SearchTransportationActivity;
import com.whut.transportation.UnloaCompletedDetailActivity;
import com.whut.transportation.UnloadDetailActivity;
import com.whut.transportation.adapter.OWinfoAdapter;
import com.whut.transportation.adapter.OWinfoAdapter.MyClickListener;
import com.whut.transportation.adapter.TransinfoAdapter;
import com.whut.transportation.entity.AllTransInfo;
import com.whut.transportation.entity.AllWeighInfo;
import com.whut.transportation.entity.OverWeightInfo;
import com.whut.transportation.entity.TransInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class OWSearchOverWeighActivity extends BaseActivity implements MyClickListener{
	
	private ListView overweighinfo;
	private TextView addMoreBtn;
	private View addMoreView;
	private List<OverWeightInfo> overWeighInfos;
	private OWinfoAdapter oWinfoAdapter;
	private int curpage=1;
	
	private SharedPreferences preferences;
	private Intent intent;

	/**
	 * 进入页面前加载数据
	 */
	@Override
	protected void onResume() {
		if (oWinfoAdapter != null) {
			curpage = 1;
			overWeighInfos.clear();
			oWinfoAdapter.notifyDataSetChanged();
			getData(false);
		} else {
			intent = getIntent();
			getData(false);
		}
		super.onResume();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add_more:
			getData(false);
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	
	//获取数据
	private void getData(final boolean isRefresh) {
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.FIND_SEARCH_WEIGN_INFO;
		vo.context = mContext;
		
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("curpage", String.valueOf(curpage));
		req.put("pagerecord", "8");
		req.put("owSearchRequestInfo.owstaffId", preferences.getString("userId", null));
		String searchTime = intent.getStringExtra("searchTime");
		req.put("owSearchRequestInfo.searchTime", searchTime);
		String trainId = intent.getStringExtra("trainId");
		if(trainId.length()>0)
			req.put("owSearchRequestInfo.trainId", trainId);
		String searchContext = intent.getStringExtra("searchContext");
		if(searchContext.length()>0)
			req.put("owSearchRequestInfo.serialId", searchContext);
		vo.requestDataMap = req;
		vo.parser = new JSONParser(AllWeighInfo.class);
		
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
							showProgressDialog(getResources().getString(
								R.string.loading_content));
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						int code = (Integer)paramObject.get("code");
						if(code == 1){
							AllWeighInfo allWeighInfo = (AllWeighInfo) paramObject.get("result");
							if(isRefresh)
								overWeighInfos.clear();
							overWeighInfos.addAll(allWeighInfo.ListWeighInfo);
							if(overweighinfo.getAdapter()==null){
								oWinfoAdapter = new OWinfoAdapter(mContext, overWeighInfos, R.layout.activity_detail_owinfo_item,OWSearchOverWeighActivity.this);
								overweighinfo.setAdapter(oWinfoAdapter);
							} else {
								oWinfoAdapter.notifyDataSetChanged();
							}
							if(curpage>=(Integer)paramObject.get("allpage")){
								addMoreBtn.setEnabled(false);
							}
							curpage++;
						} else {
							Toast.makeText(mContext, "目前数据为空", Toast.LENGTH_LONG).show();
						}
					}

					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

	@Override
	protected void findViewById() {
		overweighinfo = (ListView) findViewById(R.id.overweighinfo);
		addMoreBtn = (TextView) addMoreView.findViewById(R.id.btn_add_more);
	}

	@Override
	protected void afterFindViewId() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_search_overweigh);
		addMoreView = View.inflate(mContext, R.layout.add_more, null);
		setTitleBarView(true, "搜索", -1, true);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
	}

	@Override
	protected void processLogic() {
		overWeighInfos = new ArrayList<OverWeightInfo>();
		overweighinfo.addFooterView(addMoreView);
	}

	@Override
	protected void setListener() {
		addMoreBtn.setOnClickListener(this);
		overweighinfo.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				if(overWeighInfos.size()>position){
					OverWeightInfo overWeightInfo = overWeighInfos.get(position);
						if(overWeightInfo.getComplete() == 0){//未完成的卸车数据
							Intent overWeightDetailActivity = new Intent(OWSearchOverWeighActivity.this, OverWeightDetailActivity.class);
							overWeightDetailActivity.putExtra("overWeightInfo", overWeighInfos.get(position));
							startActivity(overWeightDetailActivity);
						} else {//已完成的卸车数据
							Intent oWCompletedDetailActivity = new Intent(OWSearchOverWeighActivity.this,
									OWCompletedDetailActivity.class);
							oWCompletedDetailActivity.putExtra("overWeightInfo", overWeightInfo);
							startActivity(oWCompletedDetailActivity);
						}
					
					}
				
			}
		});
	}

	@Override
	protected void getDataAgain() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean clickListener(View v, Integer tag) {
		// TODO Auto-generated method stub
		return false;
	}

}
